<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Develop;

use Srv\Apps\Apis\Develop\Ctl\AppVersionCtl;
use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Proto\DTO\Develop\AppVersionAddDTO;
use Srv\Apps\Proto\DTO\Develop\AppVersionEditDTO;
use Srv\Apps\Proto\DTO\Develop\AppVersionInfoDTO;
use Srv\Apps\Proto\DTO\Develop\AppVersionListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Develop\AppVersionListVO;
use Srv\Apps\Proto\VO\Develop\AppVersionInfoVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class AppVersionAct extends ActionAbstract
{
    /**
     * @url /app/version/list
     * @auth appVersionList
     * @req Srv.Apps.Proto.DTO.Develop.AppVersionListDTO
     * @rsp Srv.Apps.Proto.VO.Develop.AppVersionListVO
     * App版本列表
     */
    public function appVersionList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof AppVersionListDTO) || !($VOClass instanceof AppVersionListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = AppVersionCtl::tag()->getAppVersionList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /app/version/info
     * @auth appVersionInfo
     * @req Srv.Apps.Proto.DTO.Develop.AppVersionInfoDTO
     * @rsp Srv.Apps.Proto.VO.Develop.AppVersionInfoVO
     * App版本信息
     */
    public function appVersionInfo(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($RouteConfData);
        if(!($DTOClass instanceof AppVersionInfoDTO) || !($VOClass instanceof AppVersionInfoVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $vrlrId     = $DTOClass->getVrlrId();
        $VOClass    = AppVersionCtl::tag()->getAppVersionInfo($vrlrId);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /app/version/add
     * @auth appVersionAdd
     * @req Srv.Apps.Proto.DTO.Develop.AppVersionAddDTO
     * App版本新增
     */
    public function appVersionAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof AppVersionAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData    = $RouteConfData->getParamsResData($DTOClass);
        $vrlrId     = AppVersionCtl::tag()->appVersionAdd($setData);
        $LogCtl     = LogCtl::tag();
        $logData    = $setData;
        if($vrlrId < 1) {
            $errMsg = 'App版本新增失败';
            $LogCtl->logAdd(LogCtl::APP_VERSION_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg     = 'App版本新增成功';
        $LogCtl->logAdd(LogCtl::APP_VERSION_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto();
    }

    /**
     * @url /app/version/edit
     * @auth appVersionEdit
     * @req Srv.Apps.Proto.DTO.Develop.AppVersionEditDTO
     * App版本编辑
     */
    public function appVersionEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof AppVersionEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $vrlrId         = intval($DTOClass->getVrlrId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $AppVersionCtl  = AppVersionCtl::tag();
        $appVersionSrc  = $AppVersionCtl->getAppVersionSrc($vrlrId);
        $LogCtl         = LogCtl::tag();
        $logData        = $setData;
        if(!isset($appVersionSrc['vrlr_id'])) {
            $errMsg     = 'App版本不存在';
            $LogCtl->logAdd(LogCtl::APP_VERSION_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $result         = $AppVersionCtl->appVersionEdit($vrlrId, $setData);
        $logData        = ['new' => $setData, 'old' => $appVersionSrc];
        if(!$result){
            $errMsg     = 'App版本编辑失败';
            $LogCtl->logAdd(LogCtl::APP_VERSION_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = 'App版本编辑成功';
        $LogCtl->logAdd(LogCtl::APP_VERSION_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto();
    }
}